﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
using Newtonsoft.Json;
using ZF.BLL.Admin;
namespace ZF.WS.Admin.ads
{
    public partial class setIndexads : ZF.BLL.Page.MMPage
    {

        [PageArgument(Key = "key", CanNull = false)]
        public string Key { get; set; }
        public override ModuleType Module
        {
            get { return ModuleType.Ads; }
            protected set
            {
                base.Module = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Edit();
            }
        }
        public void InitControls()
        {
        }






        public void Add()
        {
            InitControls();

        }
        public void AddSubmit(object sender, EventArgs e)
        {
            //using (var dc = SiteInfo.CreateDBDataContext())
            //{
            //    var popularize = new ZF.Model.AD_Popularize()
            //    {
            //        Address = this.tbAddress.Text,
            //        AddTime = DateTime.Now,
            //        CompanyName = this.tbName.Text,
            //        Contact = this.tbContact.Text,
            //        Email = this.tbEmail.Text,
            //        Info = this.tbInfo.Text,
            //        OrderId = this.tbOrderId.Text.EmptyInt(0),
            //        Tel = this.tbTel.Text,
            //        TypeId = this.ddlType.SelectedValue.Convert<int>(),
            //        Zip = this.tbZipcode.Text,
            //        Url = this.tbUrl.Text.EmptyString(),
            //        Logo = this.SWFUpLoad1.UpLoadFileInfos.ConstituteString(";"),
            //    };


            //    try
            //    {
            //        dc.AD_Popularize.InsertOnSubmit(popularize);
            //        dc.SubmitChanges();
            //    }
            //    catch (Exception ex)
            //    {
            //        throw WarnFactory.CreateWarn(ex);
            //    }

            //    string msgKey = CTitle;

            //    SiteInfo.InsertLog("添加" + msgKey, null);
            //    WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回上一页", msgKey));
            //    link.AddWarnMessageLink(string.Format("继续添加{0}", msgKey), Request.Url.ToString());
            //    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();


            //}


        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var ads = dc.AD_Img.FirstOrDefault(p => p.AppKey == Key);
                if (ads != null)
                {
  
                    this.tbJavascript.Text = ads.JavaScript;
                    this.tbStyle.Text = ads.Style;
                    this.tbUrl.Text = ads.Url;
                    this.lvImgList.DataSource = ads.Image.EmptyString("").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                                       .Select(p => new
                                       {
                                           File = p
                                       });
                    this.lvImgList.DataBind();
                }

                InitControls();
     


            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var ads = dc.AD_Img.FirstOrDefault(p => p.AppKey == Key);
                    if (ads == null)
                    {
                        ads = new ZF.Model.AD_Img();

                    }
                    ads.Url = this.tbUrl.Text;
                    ads.Style = this.tbStyle.Text;
                    ads.JavaScript = this.tbJavascript.Text;
             
                    ads.AppKey = Key;
                    var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    var photoArr = this.SWFUpLoad1.UpLoadFileInfos.ToList();
                    photoArr.AddRange(photoList);
                    ads.Image = photoArr.FirstOrDefault() ;

                    try
                    {
                        if (ads.ImgId == 0)
                            dc.AD_Img.InsertOnSubmit(ads);
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = "页面广告";

                    SiteInfo.InsertLog("修改" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功修改一个{0}，点击确定按钮返回{0}列表", msgKey));

                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey), Request.Url.ToString());
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, WarnLink.Current).Alert();

                }


            }

        }








    }
}
